package MyGO.utils.FileUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class FileIO {
    private final static String FILE_PATH = "src/main/resources/MyGO/DataJSON/";
    private final static ObjectMapper mapper = new ObjectMapper().
            configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    // 通用方法：将对象写入JSON文件
    public static <T> long writeObjectAsJsonFile(T obj, String filename) throws IOException {
        File file = new File(FILE_PATH + filename);
        if (file.exists()) {
            file.delete();
        }
        try (FileOutputStream os = new FileOutputStream(file)) {
            file.createNewFile();
            String json = mapper.writeValueAsString(obj);
            byte[] bs = json.getBytes();
            os.write(bs);
            os.flush();
            return bs.length;
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
            throw ex;
        }
    }

    // 通用方法：从JSON文件读取对象
    public static <T> T readObjectFromJsonFile(String fileName, TypeReference<T> typeReference) throws IOException {
        File file = new File(FILE_PATH + fileName);
        if (!file.exists()) {
            return null;
        }
        try (FileInputStream is = new FileInputStream(file)) {
            byte[] bs = new byte[is.available()];
            is.read(bs);
            String json = new String(bs);
            return mapper.readValue(json, typeReference);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
            throw ex;
        }
    }
}


//public class FileIO {
//    private static final ObjectMapper objectMapper = new ObjectMapper();
//
//    public static <T> void writeObjectAsJsonFile(T data, String fileName) {
//        String FILENAME = "src/main/resources/MyGO/DataJSON/" + fileName;
//        try {
//            objectMapper.writeValue(new File(FILENAME), data);
//            System.out.println("Data successfully written to file: " + FILENAME);
//        } catch (IOException e) {
//            System.out.println("An error occurred while writing the data to file: " + e.getMessage());
//        }
//    }
//
//    public static <T> T readFromFile(String fileName, Class<T> valueType) {
//        String FILENAME = "src/main/resources/MyGO/DataJSON/" + fileName;
//        try {
//            File file = new File(FILENAME);
//            if (file.exists()) {
//                return objectMapper.readValue(file, valueType);
//            } else {
//                System.out.println("File doesn't exist: " + FILENAME);
//            }
//        } catch (IOException e) {
//            System.out.println("An error occurred while reading the data from file: " + e.getMessage());
//        }
//        return null;
//    }
//
//    public static <T> List<T> readListFromFile(String fileName, TypeReference<List<T>> typeReference) {
//        String FILENAME = "src/main/resources/MyGO/DataJSON/" + fileName;
//        try {
//            File file = new File(FILENAME);
//            if (file.exists()) {
//                return objectMapper.readValue(file, typeReference);
//            } else {
//                System.out.println("File doesn't exist: " + FILENAME);
//            }
//        } catch (IOException e) {
//            System.out.println("An error occurred while reading the data from file: " + e.getMessage());
//        }
//        return null;
//    }
//}


//public class FileIO {
//    private  final static String  FILE_PATH ="src/main/resources/MyGO/DataJSON/";
//    private  final static ObjectMapper mapper = new ObjectMapper();
//    public  static   long writeObjectAsJsonFile(Object obj,String filename) throws IOException {
////        1、判断文件是否存在
////        2、如果存在删除，否则新建
////        3、obj - 》  JSON
////        4、String 写入文件
//        File  file =new File( FILE_PATH +  filename); // 相对路径
//        if (file.exists()){
//            file.delete();
//        }
//        FileOutputStream os =null;
//        try
//        {
//            file.createNewFile();
//
//            String  json = mapper.writeValueAsString(obj);
//            byte[] bs  = json.getBytes();
//            os =new FileOutputStream(file);
//            os.write(bs);
//            os.flush();
//            return  bs.length;
//        }catch (IOException ex ){
//            System.out.println( ex.getMessage());
//        }finally {
//            if (os!=null){
//                os.close();
//            }
//        }
//        return   -1;
//
//    }
//
//    public static Object readObjectFromJsonFile(String fileName ,Class clazz) {
//        try {
//            File file = new File(FILE_PATH+fileName);
//            if (!file.exists()) {
//                return   new ArrayList();
//            }
//            FileInputStream is = null;
//            try {
//                is  =new FileInputStream(  file);
//                byte[] bs = new byte[is.available()] ;
//                is.read(bs);
//                String json =  new String(bs  );
//                return  mapper.readValue(json,clazz);
//            }
//            catch (Exception ex){
//                System.out.println(ex.getMessage());
//            }
//            finally {
//                if (is!=null){
//                    is.close();
//                }
//            }
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//}

